package 临时软件包.ui;

import com.weiawei.Dialog.EndingDialog;
import com.weiawei.Model.Chess;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ChessBoard extends JPanel implements MouseListener {

    public static final int GAME_OVER = 0;
    public static final int GAME_START = 1;

    public static final int hang = 19;
    public static final int lie = 19;
    // 标记是否可点击
    protected boolean clickable;
    // 棋子的坐标
    protected int chessX;
    protected int chessY;
    // 棋盘图片
    private Image chessBoardImage;
    // 棋子图片
    private Image blackChess;
    private Image whiteChess;

    //记录棋子的矩阵
    public int[][] chessSet = new int[hang][lie];

    //用户下棋的步骤轮数 : 坐标和次数
    public int stepCount ;

    protected int[][] step;


    // 设置游戏结束对话框
    protected EndingDialog endingDialog;

    /**
     * ChessBoard 的类构造函数
     * 完成度棋盘样式的初始化
     */
    public ChessBoard(){
        initImages();
        initChessSet();
        addMouseListener(this);
    }

    /**
     * 初始化棋盘中所用到的图片
     * 棋盘 黑棋 白旗
     */

    private void initImages() {
        chessBoardImage = Toolkit.getDefaultToolkit().getImage(ChessBoard.class.getClassLoader().getResource("images/gobangBoard.png"));
        blackChess = Toolkit.getDefaultToolkit().getImage(ChessBoard.class.getClassLoader().getResource("images/blackChess.png"));
        whiteChess = Toolkit.getDefaultToolkit().getImage(ChessBoard.class.getClassLoader().getResource("images/whiteChess.png"));
    }

    /**
     * 初始化棋盘的矩阵
     * 将所有棋子坐标的设为无棋的状态
     */
    public void initChessSet() {
        for(int i = 0 ; i < lie ; i++) {
            for (int j = 0; j < hang; j++) {
                chessSet[i][j] = Chess.noChess;
            }
        }
    }

    /**
     * 初始化用户下棋记录
     */
    public void initStep(){
        step = new int[1000][2];
        stepCount = 0;
    }

    /**
     * 保存用户下棋的坐标
     * @param PointA  棋的x坐标
     * @param PointB  棋的y坐标
     */
    public void saveStep(int PointA , int PointB){
        // 先下棋，再让回合+1
        stepCount++;
        step[stepCount][0] = PointA;
        step[stepCount][1] = PointB;
    }

    /**
     * step[][] 记录着每一步的下棋的棋子坐标
     * step[stepCount][0]  step[stepCount][1] 对应着棋子 在 chessSet矩阵中的x y 坐标
     * 将矩阵 当前轮次的棋子置空， 将上一步的棋子也置空，轮次调回两轮前 即可实现悔棋
     */
    public void backStep(){
        if(stepCount >= 2){
            chessSet[step[stepCount][0]][step[stepCount][1]] = 0 ;
            chessSet[step[stepCount - 1][0]][step[stepCount-1][1]] = 0;
            stepCount = stepCount - 2;
        }
    }

    /**
     * 设置开始游戏按钮的点击事件
     * @param clickable  棋盘是否可以点击
     */
    public void setClickable(boolean clickable){
        this.clickable = clickable ;
    }

    /**
     * 绘制棋盘的背景和棋子
     * @param graphics 绘图类对象
     */
    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        graphics.drawImage(chessBoardImage,0,0,null);
        for(int i = 0 ; i < hang ; i++){
            for(int j = 0 ; j < lie ; j++){
                if(chessSet[i][j] == Chess.black) {
                    graphics.drawImage(blackChess , 20 + i * 30 ,20 + j * 30 ,null);
                }else if(chessSet[i][j] == Chess.white){
                    graphics.drawImage(whiteChess,20 + i * 30,20 + j * 30,null);
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
